package org.cos.verifier.process;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.cos.verifier.utility.IOWrapper;
class RuleTable {
	public RuleTable(){
		
	}
}

public class RuleManager {
	private String dbfile;
	private Hashtable rules;
	public RuleManager() {
		rules = new Hashtable();
		dbfile = null;
	}
	static RuleManager instance;
	public static RuleManager get_inst() {
		if(instance == null)
			instance = new RuleManager();
		return instance;
	} 
	
	public void set_db(String dbfile) {
		this.dbfile = dbfile;
	}
	
	public void load() {
		byte[] raw =null;
		try {
			raw = IOWrapper.get_inst().read_file(dbfile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		ByteArrayInputStream bin = new ByteArrayInputStream(raw);
		DataInputStream din = new DataInputStream(bin);
		while (true){
			Rule r = new Rule();
			try {
				r.name = din.readUTF();
				r.describ = din.readUTF();
				r.content = din.readUTF();
				if (r.name.length() == 0){
					return;
				}
			} catch (EOFException e){
				break;
			} catch (IOException e) {
				e.printStackTrace();
			}
			this.add_rule(r);
		}
		try {
			din.close();
			bin.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void dump(){
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		DataOutputStream dout = new DataOutputStream(bout);
		for (Enumeration enumx = rules.elements(); enumx.hasMoreElements();){
			Rule r = (Rule) enumx.nextElement();
			try {
				dout.writeUTF(r.name);
				dout.writeUTF(r.describ);
				dout.writeUTF(r.content);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		byte[] raw = bout.toByteArray();
		try {
			IOWrapper.get_inst().write_file(dbfile, raw);
			dout.close();
			bout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public int get_rule_count() {
		return this.rules.size();
 	}
	


	public void shutdown(){ 
	}

	public int add_rule(Rule rule)  {
		if (rules.containsKey(rule.name)){
			return -1;
		}
		rules.put(rule.name, rule);
		return 0;
	}
	
	public boolean lookup_rule(String name)  {
		return rules.containsKey(name);
	}
	
	public Rule set_rule(Rule rule) {
		rules.put(rule.name, rule);
		return null;
	}
	
	public Rule get_rule(String name)   {
		if (lookup_rule(name)) {
			return (Rule) rules.get(name);
		}
		return null;	
	}
	
	public Hashtable get_all_rule() {
		return rules;
	}
	
	public Rule remove_rule(String name)  {
		if (lookup_rule(name)) {
			return (Rule) rules.remove(name);
		}
		return null;	
	}
	public Vector remove_all_rule() {
		rules.clear();
		return null;
	}
	public int import_db(String filename) {
		//TODO 从XML导入
		return 0;
	}
	public int export_db(String filename) {
		//TODO 导出到XML
		return 0;
	}
}

